<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Vote
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 * @version    $Id: Menus.php 2011-03-09 08:45:41Z idris $
 * @author     Idris
 */

/**
 * @category   Application_Extensions
 * @package    Vote
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 */

class Vote_Plugin_Menus {
	public function canCreateVoting($row) {
		// Must be logged in
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		if (! $viewer || ! $viewer->getIdentity ()) {
			return false;
		}
		
		if (! Engine_Api::_ ()->vote ()->isAllowedCreation ( $viewer )) {
			return false;
		}
		
		return $row;
	}
	
	public function canViewVoting($row) {
		$viewer = Engine_Api::_ ()->user ()->getViewer (); // he_pa($row->toArray()); exit();
		

		if (! Engine_Api::_ ()->authorization ()->isAllowed ( 'vote_challenge', $viewer, 'view' )) {
			return false;
		}
		
		return $row;
	}
	
	public function onMenuInitialize_VoteMainPhotoIndex($row) {
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		
		if (! Engine_Api::_ ()->authorization ()->isAllowed ( 'vote_challenge', $viewer, 'view' )) {
			return false;
		}
		
		return array ('label' => 'Browse Votings', 'route' => 'vote_general', 'params' => array ('action' => 'index', 'controller' => 'photo' ) );
	}
	
	public function onMenuInitialize_VoteProfileTell($row) {
		if (! Engine_Api::_ ()->core ()->hasSubject ()) {
			return false;
		}
		
		$subject = Engine_Api::_ ()->core ()->getSubject ();
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		
		if (! $viewer->getIdentity ()) {
			return false;
		}
		
		return array ('label' => 'Tell a Friend', 'route' => 'kowcore_extended', 'icon' => 'application/modules/Kowcore/externals/images/tell.png', 'class' => 'smoothbox', 'params' => array ('action' => 'promote', 'controller' => 'promotion', 'format' => 'smoothbox' ) );
	}
	
	public function onMenuInitialize_VoteProfileEdit($row) {
		if (! Engine_Api::_ ()->core ()->hasSubject ()) {
			return false;
		}
		
		$subject = Engine_Api::_ ()->core ()->getSubject ();
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		
		if ($subject->isStarted ()) {
			return false;
		}
		
		if (! Engine_Api::_ ()->authorization ()->isAllowed ( $subject, $viewer, 'edit' )) {
			return false;
		}
		
		return array ('label' => 'Edit Voting', 'route' => 'vote_general', 'icon' => 'application/modules/Kowcore/externals/images/edit.png', 'params' => array ('action' => 'edit', 'controller' => $subject->type, 'challenge_id' => $subject->getIdentity () ) );
	}
	
	public function onMenuInitialize_VoteProfileStart($row) {
		if (! Engine_Api::_ ()->core ()->hasSubject ()) {
			return false;
		}
		
		$subject = Engine_Api::_ ()->core ()->getSubject ();
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		
		if ($subject->isStarted ()) {
			return false;
		}
		
		if ($subject->isFinished ()) {
			return false;
		}
		
		if ($subject->is_started == 1 || $subject->is_finished == 1) {
			return false;
		}
		
		if (! $subject->isOwner ( $viewer )) {
			return false;
		}
		
		return array ('label' => 'Start Voting', 'route' => 'vote_general', 'class' => 'smoothbox', 'icon' => 'application/modules/Kowcore/externals/images/start.png', 'params' => array ('action' => 'start', 'controller' => 'vote', 'challenge_id' => $subject->getIdentity (), 'format' => 'smoothbox' ) );
	}
	
	public function onMenuInitialize_VoteProfileFinish($row) {
		if (! Engine_Api::_ ()->core ()->hasSubject ()) {
			return false;
		}
		
		$subject = Engine_Api::_ ()->core ()->getSubject ();
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		
		if (! $subject->isStarted ()) {
			return false;
		}
		
		if ($subject->is_started != 1 || $subject->is_finished != 0) {
			return false;
		}
		
		if (! $subject->isOwner ( $viewer )) {
			return false;
		}
		
		return array ('label' => 'Finish Voting', 'route' => 'vote_general', 'class' => 'smoothbox', 'icon' => 'application/modules/Kowcore/externals/images/finish.png', 'params' => array ('action' => 'finish', 'controller' => 'vote', 'challenge_id' => $subject->getIdentity (), 'format' => 'smoothbox' ) );
	}
	
	public function onMenuInitialize_VoteProfileStyle($row) {
		if (! Engine_Api::_ ()->core ()->hasSubject ()) {
			return false;
		}
		
		$subject = Engine_Api::_ ()->core ()->getSubject ();
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		
		if ($subject->isStarted ()) {
			return false;
		}
		
		if (! $viewer->getIdentity () || ! $subject->authorization ()->isAllowed ( $viewer, 'edit' )) {
			return false;
		}
		
		if (! $subject->authorization ()->isAllowed ( $viewer, 'style' )) {
			return false;
		}
		
		return array ('label' => 'Edit Voting Style', 'route' => 'vote_general', 'class' => 'smoothbox', 'icon' => 'application/modules/Kowcore/externals/images/style.png', 'params' => array ('action' => 'style', 'controller' => 'vote', 'challenge_id' => $subject->getIdentity (), 'format' => 'smoothbox' ) );
	}
	
	public function onMenuInitialize_VoteProfileReport() {
		return false;
	}
	
	public function onMenuInitialize_VoteProfileShare() {
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		$subject = Engine_Api::_ ()->core ()->getSubject ();
		
		if ($subject->getType () !== 'vote_challenge') {
			throw new Exception ( 'This challenge does not exist.' );
		}
		
		if (! $viewer->getIdentity ()) {
			return false;
		}
		
		return array ('label' => 'Share This Voting', 'icon' => 'application/modules/Kowcore/externals/images/share.png', 'class' => 'smoothbox', 'route' => 'default', 'params' => array ('module' => 'activity', 'controller' => 'index', 'action' => 'share', 'type' => $subject->getType (), 'id' => $subject->getIdentity (), 'format' => 'smoothbox' ) );
	}
	
	public function onMenuInitialize_VoteProfileDelete() {
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		$subject = Engine_Api::_ ()->core ()->getSubject ();
		
		if ($subject->isStarted ()) {
			return false;
		}
		
		if (! $viewer->getIdentity () || ! $subject->authorization ()->isAllowed ( $viewer, 'delete' )) {
			return false;
		}
		
		return array ('label' => 'Delete Voting', 'icon' => 'application/modules/Kowcore/externals/images/remove.png', 'route' => 'vote_general', 'class' => 'smoothbox', 'params' => array ('controller' => 'vote', 'action' => 'delete', 'challenge_id' => $subject->getIdentity (), 'format' => 'smoothbox' ) );
	}

}